package com.hanson.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.Const;
import com.hanson.common.bean.DeleteParam;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.common.util.EasyExcelUtil;
import com.hanson.common.util.KuaidiUtil;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.OrderBean;
import com.hanson.entity.OrderRefundBean;
import com.hanson.entity.UserBean;
import com.hanson.service.OrderRefundService;
import com.hanson.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private OrderRefundService orderRefundService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap, HttpServletRequest request, String orderType) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
                modelMap.put("userType", userBean.getUserType());
            }
        }
        modelMap.put("order", orderService.statisOrder(merId));
        modelMap.put("orderType", orderType);
        return "admin/order/index";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(ModelMap modelMap, String id) {
        OrderBean orderBean = orderService.selectDetail(id);
        modelMap.put("data", orderBean);
        if (null != orderBean && StringUtils.isNotEmpty(orderBean.getCourierNo())) {
            String no = orderBean.getCourierNo();
            if (no.startsWith("SF")) {
                no = no + ":" + no.substring(no.length() - 4, no.length());
            }
            if (null == Const.kuaiDiCache.getIfPresent(no)) {
                JSONObject jsonObject = KuaidiUtil.getKuaiDiDetail(no);
                Integer status = jsonObject.getInteger("status");
                if (0 == status) {
                    modelMap.put("courier", jsonObject.getJSONObject("result").getJSONArray("list"));
                }
            } else {
                modelMap.put("courier", Const.kuaiDiCache.getIfPresent(no));
            }
        }
        return "admin/order/detail";
    }

    @RequestMapping(value = "/refund", method = RequestMethod.GET)
    public String refund(ModelMap modelMap, String orderId) {
        modelMap.put("data", orderRefundService.detail(orderId));
        return "admin/order/refund";
    }

    @RequestMapping(value = "/refused", method = RequestMethod.GET)
    public String refused(ModelMap modelMap, String orderId) {
        modelMap.put("data", orderRefundService.detail(orderId));
        return "admin/order/refused";
    }

    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage refund(OrderRefundBean orderRefundBean) {
        ResultMessage resultMessage = new ResultMessage();
        orderRefundService.agreeRefund(orderRefundBean);
        resultMessage.setMsg("退款成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/refused", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage refused(OrderRefundBean orderRefundBean) {
        ResultMessage resultMessage = new ResultMessage();
        orderRefundService.notAgreeRefund(orderRefundBean);
        resultMessage.setMsg("拒绝退款成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap, String id) {
        modelMap.put("data", orderService.selectDetail(id));
        return "admin/order/modify";
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<OrderBean> listData(int limit, int page, OrderBean orderBean, HttpServletRequest request) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
            }
        }
        orderBean.setMerId(merId);
        Page<OrderBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(orderService.listOrders(pageInfo, orderBean));
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modifyData(OrderBean orderBean) {
        ResultMessage resultMessage = new ResultMessage();
        orderService.updateOrderInfo(orderBean);
        resultMessage.setMsg("修改订单信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(@RequestBody DeleteParam deleteParam) {
        ResultMessage resultMessage = new ResultMessage();
        orderService.deleteOrders(deleteParam.getIds());
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void download(OrderBean orderBean, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
            }
        }
        orderBean.setMerId(merId);
        EasyExcelUtil.writeExcel(response, orderService.listOrdersAll(orderBean), "订单数据" + System.currentTimeMillis(), "订单", OrderBean.class);
    }


}
